//
//  BuyViewController.m
//  LoveTrail
//
//  Created by giantumbrella on 15/6/3.
//  Copyright (c) 2015年 赵娟. All rights reserved.
//

#import "BuyViewController.h"
#import "CommonMacro.h"
#import "PopViewController.h"
#import "ProductDetailViewController.h"
#import "ProductInfoModel.h"
#import "ProductInfoCell.h"
@interface BuyViewController ()
{
PopViewController *popVC;
}
@property (nonatomic, weak)UICollectionView *collectionView;
@property (nonatomic, strong)NSArray *JsonArray;
@end

@implementation BuyViewController

- (NSMutableArray *)productInfoModelArray
{
    if (_productInfoModelArray==nil) {
        _productInfoModelArray = [[NSMutableArray alloc] init];
    }
    return _productInfoModelArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //假的但数据格式非常标准的json数据
    self.JsonArray = @[
                       @{@"imageUrl":@"http://pic4.nipic.com/20090803/2618170_100713057_2.jpg",@"productTitle":@"韩国想啊想啊啊撒大声地爱上",@"currentPrice":@"40",@"originalPrice":@"100",@"saledNum":@"100"},
                       @{@"imageUrl":@"http://pic4.nipic.com/20090803/2618170_100713057_2.jpg",@"productTitle":@"生活很美好",@"currentPrice":@"10",@"originalPrice":@"10",@"saledNum":@"10"},
                       
                       @{@"imageUrl":@"http://pic4.nipic.com/20090803/2618170_100713057_2.jpg",@"productTitle":@"人生入戏",@"currentPrice":@"20",@"originalPrice":@"20",@"saledNum":@"20"},
                       
                       @{@"imageUrl":@"http://pic4.nipic.com/20090803/2618170_100713057_2.jpg",@"productTitle":@"纯棉不觉晓",@"currentPrice":@"30",@"originalPrice":@"30",@"saledNum":@"30"},
                       
                       @{@"imageUrl":@"http://pic4.nipic.com/20090803/2618170_100713057_2.jpg",@"productTitle":@"处处闻啼鸟",@"currentPrice":@"40",@"originalPrice":@"40",@"saledNum":@"40"},
                       
                       @{@"imageUrl":@"http://pic4.nipic.com/20090803/2618170_100713057_2.jpg",@"productTitle":@"夜来风雨声",@"currentPrice":@"50",@"originalPrice":@"50",@"saledNum":@"50"},
                       
                       @{@"imageUrl":@"http://pic4.nipic.com/20090803/2618170_100713057_2.jpg",@"productTitle":@"花落知多少",@"currentPrice":@"60",@"originalPrice":@"60",@"saledNum":@"60"}
                       ];
    for (int i=0; i<self.JsonArray.count; i++) {
        ProductInfoModel *productInfoModel = [[ProductInfoModel alloc] init];
        [productInfoModel decodeDic:self.JsonArray[i]];
        [self.productInfoModelArray addObject:productInfoModel];
    }
    [self.collectionView reloadData];
   
    //弹出的  //popView
    popVC = [[PopViewController alloc] init];
    
    //添加tabbar和search
    [self addSubViews];
    
    //添加瀑布流照片
    [self pictureAndOthers];
}

#pragma mark 跳出菜单
#pragma mark 弹出视图

-(void)popView:(UIBarButtonItem *)sender
{
    [popVC popView];
}

#pragma mark 摆放tabBar和Search
- (void)addSubViews
{
    //左侧按钮
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"head_type"] style:UIBarButtonItemStyleDone target:self action:@selector(popView:)];
    self.navigationItem.leftBarButtonItem = leftButton;
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    //右侧按钮
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"_0001_购物车"] style:UIBarButtonItemStylePlain target:self action:@selector(nextView)];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    
    //搜索栏
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(100.0f,10, SCREEN_WIDTH*120.320, 40.0f)];
    self.searchBar.delegate =self;
    self.searchBar.placeholder = @"搜索";
    self.searchBar.showsCancelButton = NO;
    self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
    self.searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
    self.searchBar.keyboardType = UIKeyboardTypeDefault;
    self.navigationItem.titleView = self.searchBar;
}


#pragma mark 瀑布流数据图片 其他
- (void)pictureAndOthers
{
    //解析数据
    NSString *path = [[NSBundle mainBundle]pathForResource:@"Data" ofType:@"json"];
    NSString *string = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *array = [string objectFromJSONString];
    
    NSMutableArray *arrayImage = [[NSMutableArray alloc]init];
    self.selfImageView = [[SelfImageVIew alloc] init];
    
    for (int i=0; i<[array count]; i++) {
        NSDictionary *dataD = [array objectAtIndex:i];
        if (dataD) {
            ImageInfo *imageInfo = [[ImageInfo alloc]initWithDictionary:dataD];
            [arrayImage addObject:imageInfo];
            
//              self.selfImageView.productInfoModel = self.productInfoModelArray[i];
        }
    }
    
    NSLog(@"%@",arrayImage);
    
    self.waterView = [[ImageWaterView alloc]initWithDataArray:arrayImage withFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
   
    self.waterView.delegate = self;
    
    //添加上拉加载更多
    __weak BuyViewController *blockSelf = self;
    [self.waterView addInfiniteScrollingWithActionHandler:^{
        NSLog(@"上拉刷新");
//          blockSelf.waterView.frame  = CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT);
        //使用GCD开启一个线程，使圈圈转2秒
        int64_t delayInSeconds = 1.0;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            [blockSelf.waterView loadNextPage:arrayImage];
            [blockSelf.waterView.infiniteScrollingView stopAnimating];
        });
    }];
    //添加下拉刷新
    [self.waterView addPullToRefreshWithActionHandler:^{
        NSLog(@"下拉更新");
//         blockSelf.waterView.frame  = CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT);
        //使用GCD开启一个线程，使圈圈转2秒
        int64_t delayInSeconds = 1.0;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            [blockSelf.waterView refreshView:arrayImage];
            [blockSelf.waterView.pullToRefreshView stopAnimating];
        });
    }];
    [self.view addSubview:self.waterView];
}

#pragma mark clickIntoNewViewDelegate
- (void)clickIntoNewView
{
    ProductDetailViewController *productDetail = [[ProductDetailViewController alloc] init];
    UINavigationController *productDNavi = [[UINavigationController alloc] initWithRootViewController:productDetail];
    [self presentViewController:productDNavi animated:YES completion:nil];
    
}

#pragma  mark 购物车上 按钮点击后
- (void)nextView
{
    NSLog(@"点击购物车按钮啦啦");
}


#pragma mark 取消第一相应
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [self.searchBar resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
